package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.advertisment.model.ActiveTranData;
import com.loongtech.bi.advertisment.service.AdvDayDataAllService;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * ClassName: AdvDayAllDataAction
 * 
 * @Description: 市场数据分析 —— 广告日数据
 * @author liuchen
 * @date 2017-03-28 下午2:23:49
 */

@Controller("adDayAllReportAction")
@Scope("view")
public class AdvDayAllDataAction extends BaseAdvAction {
	private static final long serialVersionUID = 1L;

	private String dateRadio = "7"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY)); // 自选时间：开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY)); // 自选时间：结束时间

	private List<ActiveTranData> activeTranDatas;

	@Resource(name = "advDayDataAllService")
	private AdvDayDataAllService service;

	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		activeTranDatas = new ArrayList<>();
		try {
			String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time
					.currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY))).getTime()))); // 开始时间
			String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time
					.currentTime() - Time.DAY))).getTime()))); // 结束时间
			activeTranDatas = service.getActiveTranDatas(startDay, endDay, selectedAffiliation, selectedChannelName, selectedAgent, selectedPlatform,
					selectedPaytype, selectedAdvPosition);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Integer getSumClkEquipment() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getClkEquipment();
		}
		return total;
	}

	public Integer getSumActEquipment() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getActEquipment();
		}
		return total;
	}

	public Integer getSumNewActive() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getNewActive();
		}
		return total;
	}

	public Integer getSumNewEquipment() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getNewEquipment();
		}
		return total;
	}

    public Integer getSumNum() {
        int total = 0;
        for (ActiveTranData a : activeTranDatas) {
            total += a.getNum();
        }
        return total;
    }

    public String getSumTpr() {
        DecimalFormat df = new DecimalFormat("#.##");
        int sumNewAccount = getSumNewAccount();
        return sumNewAccount != 0 ? df.format((double) getSumNum() / sumNewAccount) : "0";
    }

	public Integer getSumNewAccount() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getNewAccount();
		}
		return total;
	}

	public Integer getSumNewPay() {
		int total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getNewPay();
		}
		return total;
	}

	public String getSumPayrate() {
		DecimalFormat df = new DecimalFormat("#.##");
		int totalPayUser = 0;
		int totalNewUser = 0;
		for (ActiveTranData a : activeTranDatas) {
			totalPayUser += a.getNewPayUser();
			totalNewUser += a.getNewAccount();
		}
		return totalNewUser != 0 ? df.format((double) totalPayUser / totalNewUser) : "0";
	}

	public Integer getSumDiscount() {
		Integer total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getDiscount();
		}
		return total;
	}

	public String getSumCpa() {
		DecimalFormat df = new DecimalFormat("#.##");
		double totalDiscount = 0.0;
		int totalNewUser = 0;
		for (ActiveTranData a : activeTranDatas) {
			totalDiscount += a.getDiscount();
			totalNewUser += a.getNewAccount();
		}
		return totalNewUser != 0 ? df.format((double) totalDiscount / totalNewUser) : "0";
	}

	public int getSumTotalpay() {
		int totalpay = 0;
		for (ActiveTranData a : activeTranDatas) {
			totalpay += a.getTotalpay();
		}
		return totalpay;
	}

	public String getSumRoi() {
		DecimalFormat df = new DecimalFormat("#.##");
		Integer total = 0;
		for (ActiveTranData a : activeTranDatas) {
			total += a.getDiscount();
		}
		String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time
				.currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY))).getTime()))); // 开始时间
		String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time.currentTime()
				- Time.DAY))).getTime()))); // 结束时间
		Integer roi = 0;
		try {
			roi = service.getAllRoi(startDay, endDay, selectedAffiliation, selectedChannelName, selectedAgent, selectedPlatform, selectedPaytype,
					selectedAdvPosition);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return total != 0 ? df.format((double) roi * 100 / total) : "0";
	}

	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

	public List<ActiveTranData> getActiveTranDatas() {
		return activeTranDatas;
	}

	public void setActiveTranDatas(List<ActiveTranData> activeTranDatas) {
		this.activeTranDatas = activeTranDatas;
	}
}
